/**
 * User: maqun
 * Date: 2015/12/18
 * Time: 14:42
 *
 */
var _this = {
    checkUserId:function(id,form){
        var url = $(form).attr('url');
        $.post(url,{user_id:id.val()},
            function(info){
                $(form+" span[node-type='span-user-id']").text(info.info);
                $(form+" span[node-type='span-user-id']").show();
                $(form+" span[node-type='span-user-id']").attr('v',info.status);
            })
    },
    checkOldPass:function(pass,form){
        if(pass.val() == ''){
            $(form+" span[node-type='span-old-pass']").text('true');
            $(form+" span[node-type='span-old-pass']").hide();
        }else{
            var url = $(form).attr('url');
            $.post(url,{old_pass:pass.val()},
                function(info){
                    $(form+" span[node-type='span-old-pass']").text(info.info);
                    $(form+" span[node-type='span-old-pass']").show();
                    $(form+" span[node-type='span-old-pass']").attr('v',info.status);
                })
        }
    },
    checkNickName:function(name,form){
        var nick_name = $.trim(name.val());
        if(nick_name.length>=30){
            $(form+" span[node-type='span-nick-name']").html('No more than 30 characters');
            $(form+" span[node-type='span-nick-name']").show();
            return false;
        }else if(nick_name == ''){
            return true;
        }else{
            $(form+" span[node-type='span-nick-name']").html('Allow');
            $(form+" span[node-type='span-nick-name']").show();
            return true;
        }
    },
    checkPass:function(password,form){
        var pass    = password.val();
        if(pass==''){
            $(form+" span[node-type='span-password']").html('Not allowed to be empty');
            $(form+" span[node-type='span-password']").show();
            return false;
        }else if(pass.length<=6){
            $(form+" span[node-type='span-password']").html('Password should be Longer than 6!');
            $(form+" span[node-type='span-password']").show();
            return false;
        }else{
            $(form+" span[node-type='span-password']").html('Allow');
            $(form+" span[node-type='span-password']").show();
            return true;
        }
    },
    checkRepeatPass:function(password,repeat_pass,form){
        var pass    = password.val();
        var re_pass = repeat_pass.val();
        if(pass!=re_pass){
            $(form+" span[node-type='span-repeat-pass']").html('The passwords you typed do not match');
            $(form+" span[node-type='span-repeat-pass']").show();
            return false;
        }else{
            $(form+" span[node-type='span-repeat-pass']").html('Allow');
            $(form+" span[node-type='span-repeat-pass']").show();
            return true;
        }
    },
    //checkPass:function(password,repeat_pass,form){
    //    var pass    = password.val();
    //    var re_pass = repeat_pass.val();
    //    if(pass==''){
    //        $(form+" span[node-type='span-password']").html('Not allowed to be empty');
    //        $(form+" span[node-type='span-password']").show();
    //        return false;
    //    }else if(pass.length<=6){
    //        $(form+" span[node-type='span-password']").html('Password should be Longer than 6!');
    //        $(form+" span[node-type='span-password']").show();
    //        return false;
    //    }else if(pass!=re_pass){
    //        $(form+" span[node-type='span-repeat-pass']").html('The passwords you typed do not match');
    //        $(form+" span[node-type='span-repeat-pass']").show();
    //        return false;
    //    }else{
    //        $(form+" span[node-type='span-password']").html('Allow');
    //        $(form+" span[node-type='span-password']").show();
    //        $(form+" span[node-type='span-repeat-pass']").html('Allow');
    //        $(form+" span[node-type='span-repeat-pass']").show();
    //        return true;
    //    }
    //},
    checkEmail:function(femail,form){
        var email = femail.val();
        if(email != ''){
            //var exp = new regExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
            var exp  = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
            if(exp.test(email)){
                $(form+" span[node-type='span-email']").html('Allow');
                $(form+" span[node-type='span-email']").show();
                return true;
            }else{
                $(form+" span[node-type='span-email']").html('Mailbox format error');
                $(form+" span[node-type='span-email']").show();
                return false;
            }
        }else{
            return true;
        }
    }
};
$(document).ready(function(){
    //ע��
	var form_obj = {
        form_id:"#register_form",
		user_id:$("form#register_form input[name='user_id']"),
		nick_name:$("form#register_form input[name='nick_name']"),
		password:$("form#register_form input[name='password']"),
		repeat_pass:$("form#register_form input[name='repeat_pass']")
		//email:$("form#register_form input[name='email']")
	};
    form_obj.user_id.blur(function(){
        _this.checkUserId(form_obj.user_id,form_obj.form_id);
    });
    form_obj.password.blur(function(){
        _this.checkPass(form_obj.password,form_obj.form_id);
    });
    form_obj.repeat_pass.on('blur keyup','','',function(){
        _this.checkRepeatPass(form_obj.password,form_obj.repeat_pass,form_obj.form_id);
    });
	$("#sub_register").click(function(){

        _this.checkUserId(form_obj.user_id,form_obj.form_id);
		if(form_obj.user_id.val().length<=0){
            return false;
        }
        var res1 = $(form_obj.form_id+' span[node-type="span-user-id"]').attr('v');
        var res2  = _this.checkNickName(form_obj.nick_name,form_obj.form_id);
        var res3 = _this.checkPass(form_obj.password,form_obj.repeat_pass,form_obj.form_id);
        var res4 = _this.checkRepeatPass(form_obj.password,form_obj.repeat_pass,form_obj.form_id);

        if('true'==res1 && res2 && res3 && res4){
            return true;
        }
        return false;
	});
    //�޸ĸ�����Ϣ

    var form_obj1 = {
        form_id:"#edit-profile",
        old_pass:$("form#edit-profile input[name='old_pass']"),
        nick_name:$("form#edit-profile input[name='nick_name']"),
        password:$("form#edit-profile input[name='password']"),
        repeat_pass:$("form#edit-profile input[name='repeat_pass']"),
        email:$("form#edit-profile input[name='email']")
    };
    form_obj1.old_pass.blur(function(){
        _this.checkOldPass(form_obj1.old_pass,form_obj1.form_id);
    });
    form_obj1.password.blur(function(){
        _this.checkPass(form_obj1.password,form_obj1.form_id);
    });
    form_obj1.repeat_pass.on('blur keyup','','',function(){
        _this.checkRepeatPass(form_obj1.password,form_obj1.repeat_pass,form_obj1.form_id);
    });
    form_obj1.email.on('blur keyup','','',function(){
        _this.checkEmail(form_obj1.email,form_obj1.form_id);
    });
    $("#edit_submit").click(function(){

        _this.checkOldPass(form_obj1.old_pass,form_obj1.form_id);
        if(form_obj1.old_pass.val().length<=0){
            return true;
        }
        var res1 = $(form_obj1.form_id+' span[node-type="span-old-pass"]').attr('v');
        var res2 = _this.checkNickName(form_obj1.nick_name,form_obj1.form_id);
        var res3 = true;
        if(form_obj1.old_pass.val() !='' || form_obj1.password.val() !='' || form_obj1.repeat_pass.val() !='' ){
            res3 =  _this.checkPass(form_obj1.password,form_obj1.repeat_pass,form_obj1.form_id);
        }
        var res4 = _this.checkEmail(form_obj1.email,form_obj1.form_id);

        if('true'==res1 && res2 && res3 && res4){
            return true;
        }else{
            return false;
        }
    });
});
